<?php
class CustomersController extends AppController
{
    var $uses = array('Customer', 'Hotel', 'Booking');

    var $paginate = array(
        'limit' => 20,
        'order' => array(
            'Customer.first_name' => 'desc'
        )
    );

    function  beforeFilter() {
        parent::beforeFilter();
        $hide = '';
        if ($this->Session->read('Auth.User.role') != 'admin')
        {
            $hide = ' hide';
            $this->Auth->deny('delete');
        }
        $this->set('hide', $hide);
    }
    
    function index()
    {
        $all_customers = $this->Customer->find('all', array('recursive' => 2));
        $this->set('all_customers', $all_customers);

        foreach ($all_customers as $customer)
        {
            $total_days = 0;
            $total_spent = 0.0;
            foreach ($customer['Booking'] as $booking)
            {
                if ($booking['BookingStatus']['name'] != '7. Hủy')
                {
                    foreach ($booking['BookingDetail'] as $booking_detail)
                    {
                        $total_days += $booking_detail['days'];
                        if ($booking['currency'] == 'USD')
                        {
                            $total_spent += (float)$booking_detail['price'] * $booking['alo_exchange_rate'];
                        }
                        else
                        {
                            $total_spent += (float)$booking_detail['price'];
                        }
                    }
                }
            }
            $this->Customer->id = $customer['Customer']['id'];
            $this->Customer->save(array('total_days' => $total_days, 'total_spent' => $total_spent));
            $this->Customer->id = null;
        }

        if (isset($this->params['named']['query']))
        {
            $query = $this->params['named']['query'];
            //$query = htmlspecialchars_decode($query);
            $this->set('query', $query);

            $conditions = array();
            //$conditions['Customer.information LIKE'] = '%' . mysql_real_escape_string(trim($query)) . '%';
            $query_string = explode(',', $query);
            $count = 1;
            foreach ($query_string as $search_string)
            {
                $conditions[] = array('Customer.information LIKE' => "%" . mysql_real_escape_string(trim($search_string)) . "%");
                $count++;
            }

            $this->paginate = array(
                'limit' => 20,
                'order' => array(
                    'Customer.first_name' => 'desc'
                ),
                'conditions' => $conditions,
                //'conditions' => $conditions
            );
        }

        $this->Customer->recursive = 2;
        $customers = $this->paginate('Customer');
        $this->set('customers', $customers);

    }

    function add()
    {
        $customer_code = $this->AloCode->generateCustomerCode();
        $this->set('customer_code', $customer_code);

        if (!empty($this->data))
        {
            if ($this->Customer->save($this->data))
            {
                $this->flash('Tạo khách hàng thành công!', '');
            }
        }
    }

    function edit($id)
    {
        $this->Customer->id = $id;
        if (empty($this->data))
        {
            $this->data = $this->Customer->read();
        }
        else
        {
            $data = $this->Customer->read();
            if ($this->Customer->save($this->data))
            {
                $this->flash('Sửa khách hàng thành công!', '');
            }
        }
    }

    function show($id)
    {
        $customer = $this->Customer->find('first', array('conditions' => array('Customer.id' => $id), 'recursive' => 2));
        if (!empty($customer))
        {
            $this->set('customer', $customer);
        }
        else
        {
            $this->flash('Không tìm thấy khách hàng này', '/customers');
        }
    }

    function delete($id)
    {
        if ($this->Session->read('Auth.User.role') == 'admin')
        {
            $this->Customer->delete($id);
        }
        $this->redirect(array('controller' => 'customers', 'action' => 'index'));
    }
}
?>
